
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>智能指针 · GItBook笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="cpucode">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="AOSP.html" />
    
    
    <link rel="prev" href="hardware_abstraction_layer.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://blog.csdn.net/qq_44226094" target="_blank" class="custom-link">首页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    目录
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Linux驱动开发</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../linux_driver/character_device_driver.html">
            
                <a href="../linux_driver/character_device_driver.html">
            
                    
                    字符设备驱动开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../linux_driver/linux_driver.html">
            
                <a href="../linux_driver/linux_driver.html">
            
                    
                    Linux驱动开发
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">安卓底层开发</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="hardware_abstraction_layer.html">
            
                <a href="hardware_abstraction_layer.html">
            
                    
                    硬件抽象层
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="smart_pointer.html">
            
                <a href="smart_pointer.html">
            
                    
                    智能指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="AOSP.html">
            
                <a href="AOSP.html">
            
                    
                    AOSP源码开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="summary.html">
            
                <a href="summary.html">
            
                    
                    Android底层软硬件结合开发概述
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">操作系统</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../operating_system/operat_mechanism.html">
            
                <a href="../operating_system/operat_mechanism.html">
            
                    
                    操作系统的运行机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../operating_system/process.html">
            
                <a href="../operating_system/process.html">
            
                    
                    进程
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">linux一句话</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../linux_in_a_word/network_indepen.html">
            
                <a href="../linux_in_a_word/network_indepen.html">
            
                    
                    与网络无关的Linux
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Linux内核0.12</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../linux_kernel_0_12/computer_composition.html">
            
                <a href="../linux_kernel_0_12/computer_composition.html">
            
                    
                    微型计算机组成结构
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >智能指针</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <!--
 * @Author: cpu_code
 * @Date: 2020-07-12 14:09:20
 * @LastEditTime: 2020-07-13 11:48:29
 * @FilePath: \note\android_bottom\smart_pointer.md
 * @Gitee: https://gitee.com/cpu_code
 * @Github: https://github.com/CPU-Code
 * @CSDN: https://blog.csdn.net/qq_44226094
 * @Gitbook: https://923992029.gitbook.io/cpucode/
--> 
<h1 id="&#x667A;&#x80FD;&#x6307;&#x9488;">&#x667A;&#x80FD;&#x6307;&#x9488;</h1>
<ul>
<li>@Author: cpu_code</li>
<li>@Date: 2020-07-12 14:09:20</li>
<li>@LastEditTime: 2020-07-13 11:48:21</li>
<li>@FilePath: \note\android_bottom\smart_pointer.md</li>
<li>@Gitee: <a href="https://gitee.com/cpu_code" target="_blank">https://gitee.com/cpu_code</a></li>
<li>@Github: <a href="https://github.com/CPU-Code" target="_blank">https://github.com/CPU-Code</a></li>
<li>@CSDN: <a href="https://blog.csdn.net/qq_44226094" target="_blank">https://blog.csdn.net/qq_44226094</a></li>
<li>@Gitbook: <a href="https://923992029.gitbook.io/cpucode/" target="_blank">https://923992029.gitbook.io/cpucode/</a></li>
</ul>
<p>&#x5728;<code>Android</code>&#x7CFB;&#x7EDF;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6846;&#x67B6;&#x5C42;&#x4E2D;&#xFF0C; &#x6709;&#x4E00;&#x90E8;&#x5206;&#x4EE3;&#x7801;&#x662F;&#x4F7F;&#x7528;C++&#x8BED;&#x8A00;&#x5F00;&#x53D1;  .</p>
<p>&#x7528;<code>C++</code>&#x6700;&#x5BB9;&#x6613;&#x51FA;&#x9519;&#x7684;&#x5730;&#x65B9;&#x5C31;&#x662F;&#x6307;&#x9488;&#xFF0C; &#x4E00;&#x822C;&#x4E3A;&#x5FD8;&#x8BB0;&#x91CA;&#x653E;&#x6307;&#x9488;&#x6307;&#x5411;&#x7684;&#x5BF9;&#x8C61;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#xFF0C; &#x6216;&#x8005;&#x4F7F;&#x7528;&#x4E86;&#x65E0;&#x6548;&#x6307;&#x9488;&#x3002;   </p>
<p><code>C++</code>&#x6307;&#x9488;&#x4F7F;&#x7528;&#x4E0D;&#x5F53;&#xFF0C; &#x8F7B;&#x5219;&#x9020;&#x6210;&#x5185;&#x5B58;&#x6CC4;&#x6F0F;&#xFF0C; &#x91CD;&#x5219;&#x9020;&#x6210;&#x83AB;&#x540D;&#x5176;&#x5999;&#x7684;&#x903B;&#x8F91;&#x9519;&#x8BEF;&#xFF0C; &#x751A;&#x81F3;&#x7CFB;&#x7EDF;&#x5D29;&#x6E83;&#x3002;  </p>
<p>&#x56E0;&#x6B64;&#xFF0C; Android&#x7CFB;&#x7EDF;&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;C++&#x667A;&#x80FD;&#x6307;&#x9488;&#xFF0C; &#x907F;&#x514D;&#x51FA;&#x73B0;&#x6307;&#x9488;&#x4F7F;&#x7528;&#x4E0D;&#x5F53;&#x7684;&#x95EE;&#x9898;  </p>
<hr>
<p>&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x4EE5;&#x4E0A;&#x95EE;&#x9898; , &#x901A;&#x5E38;&#x901A;&#x8FC7;<strong>&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6280;&#x672F;</strong>&#x6765;&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F; </p>
<p>&#x6BCF;&#x5F53;&#x6709;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6307;&#x9488;<strong>&#x6307;&#x5411;</strong>&#x4E86;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C; &#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x5C31;<strong>&#x589E;&#x52A0;1</strong>  ; &#x6BCF;&#x5F53;&#x6709;&#x4E00;&#x4E2A;&#x6307;&#x9488;<strong>&#x4E0D;&#x518D;</strong>&#x6307;&#x5411;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C; &#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5C31;<strong>&#x51CF;&#x5C11;1</strong>  ; &#x5F53;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x4E3A;0</strong>&#x65F6;&#xFF0C; &#x5B83;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x5C31;&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x5730;<strong>&#x91CA;&#x653E;</strong>&#x4E86;  .</p>
<p>&#x667A;&#x80FD;&#x6307;&#x9488;&#x6B63;&#x662F;&#x4E00;&#x79CD;&#x80FD;&#x591F;<strong>&#x81EA;&#x52A8;&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x7684;&#x6280;&#x672F;&#x3002; &#x8FD9;&#x91CC;&#x9700;&#x8981;&#x7279;&#x522B;&#x5F3A;&#x8C03;&#x7684;&#x662F;&#xFF0C; &#x667A;&#x80FD;&#x6307;&#x9488;&#x662F;<strong>&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;</strong>&#xFF0C; &#x800C;<strong>&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x6307;&#x9488;</strong>&#xFF0C; &#x4F46;&#x662F;&#x5B83;&#x5F15;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x5B9E;&#x9645;&#x4F7F;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x3002;   </p>
<p>&#x5728;&#x667A;&#x80FD;&#x6307;&#x9488;<strong>&#x6784;&#x9020;</strong>&#x65F6;&#xFF0C; <strong>&#x589E;&#x52A0;</strong>&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#xFF1B; &#x800C;&#x5728;&#x667A;&#x80FD;&#x6307;&#x9488;<strong>&#x6790;&#x6784;</strong>&#x65F6;&#xFF0C; &#x5C31;<strong>&#x51CF;&#x5C11;</strong>&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;  .</p>
<p>&#x5F53; &#x6709;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;A&#x548C;B&#xFF0C; &#x5BF9;&#x8C61;A<strong>&#x5F15;&#x7528;</strong>&#x4E86;&#x5BF9;&#x8C61;B&#xFF0C; &#x800C;&#x5BF9;&#x8C61;B&#x4E5F;<strong>&#x5F15;&#x7528;&#x4E86;</strong>&#x5BF9;&#x8C61;A&#x3002; &#x4E00;&#x65B9;&#x9762;&#xFF0C; &#x5F53;&#x5BF9;&#x8C61;A&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#x65F6;&#xFF0C; &#x5C31;&#x53EF;&#x4EE5;&#x91CA;&#x653E;&#x5B83;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x4E86;&#xFF0C; &#x4F46;&#x662F;&#x7531;&#x4E8E;&#x5BF9;&#x8C61;B&#x4ECD;&#x7136;&#x5728;&#x5F15;&#x7528;&#x7740;&#x5B83;&#xFF0C; &#x56E0;&#x6B64;&#xFF0C; &#x6B64;&#x65F6;&#x5BF9;&#x8C61;A&#x5C31;&#x4E0D;&#x80FD;&#x88AB;&#x91CA;&#x653E;&#xFF1B; &#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C; &#x5F53;&#x5BF9;&#x8C61;B&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#x65F6;&#xFF0C; &#x5C31;&#x53EF;&#x4EE5;&#x91CA;&#x653E;&#x5B83;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x4E86;&#xFF0C; &#x4F46;&#x662F;&#x7531;&#x4E8E;&#x5BF9;&#x8C61;A&#x4ECD;&#x7136;&#x5728;&#x5F15;&#x7528;&#x7740;&#x5B83;&#xFF0C; &#x56E0;&#x6B64;&#xFF0C; &#x6B64;&#x65F6;&#x5BF9;&#x8C61;B&#x4E5F;&#x4E0D;&#x80FD;&#x88AB;&#x91CA;&#x653E;  </p>
<p>&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x4E5F;&#x662F; <strong>&#x5783;&#x573E;&#x6536;&#x96C6;</strong>(Garbage Collection)&#x7CFB;&#x7EDF;&#x6240;&#x9047;&#x5230;&#x7684;&#x7ECF;&#x5178;&#x95EE;&#x9898;&#x4E4B;&#x4E00;&#xFF0C; &#x56E0;&#x4E3A;&#x5B83;&#x4E00;&#x6B21;&#x53EA;&#x80FD;&#x6536;&#x96C6;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#x3002;  </p>
<hr>
<p>&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x4EE5;&#x4E0A;&#x95EE;&#x9898; , &#x91C7;&#x53D6;&#x53E6;&#x5916;&#x4E00;&#x79CD;<strong>&#x7A0D;&#x4E3A;&#x590D;&#x6742;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6280;&#x672F;</strong>&#x6765;&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x4E86;&#x3002; &#x8FD9;&#x79CD;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6280;&#x672F;&#x5C06;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5206;&#x4E3A;<strong>&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x548C;<strong>&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x4E24;&#x79CD;&#xFF0C; &#x5176;&#x4E2D;&#xFF0C; <strong>&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x53EA;&#x53D7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x63A7;&#x5236;</strong>&#x3002;  </p>
<p>&#x5728;&#x201C;&#x7236;-&#x5B50;&#x201D;&#x5173;&#x7CFB;&#x4E2D;&#xFF0C;  &#x201C;&#x7236;&#x201D;&#x5BF9;&#x8C61;&#x901A;&#x8FC7;<strong>&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x6765;&#x5F15;&#x7528;&#x201C;&#x5B50;&#x201D;&#x5BF9;&#x8C61;&#xFF1B;  &#x201C;&#x5B50;&#x201D;&#x5BF9;&#x8C61;&#x901A;&#x8FC7;<strong>&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x6765;&#x5F15;&#x7528;&#x201C;&#x7236;&#x201D;&#x5BF9;&#x8C61;&#x3002;   </p>
<p><img src="https://gitee.com/cpu_code/picture_bed/raw/master//20200712144527.png" alt="image-20200712144527052"></p>
<p>&#x5047;&#x8BBE;&#x5BF9;&#x8C61;A&#x4E3A; &#x7236; , &#x5BF9;&#x8C61;B&#x4E3A;&#x5B50;&#x3002; &#x5BF9;&#x8C61;A&#x901A;&#x8FC7;<strong>&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x6765;&#x5F15;&#x7528;&#x5BF9;&#x8C61;B&#xFF0C; &#x800C;&#x5BF9;&#x8C61;B&#x901A;&#x8FC7;<strong>&#x5F31;&#x667A;&#x80FD;&#x6307;&#x9488;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x6765;&#x5F15;&#x7528;&#x5BF9;&#x8C61;A&#x3002; &#x5F53;&#x5BF9;&#x8C61;A&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#x65F6;&#xFF0C; &#x5BF9;&#x8C61;A&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;<strong>&#x4E0D;&#x53D7;&#x5BF9;&#x8C61;B</strong>&#x7684;&#x5F71;&#x54CD;&#xFF0C; &#x6B64;&#x65F6;&#x5BF9;&#x8C61;A&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x5730;<strong>&#x91CA;&#x653E;</strong>&#x3002; &#x5728;&#x91CA;&#x653E;&#x5BF9;&#x8C61;A&#x65F6;&#xFF0C; &#x540C;&#x65F6;&#x4E5F;&#x4F1A;<strong>&#x91CA;&#x653E;</strong>&#x5B83;&#x5BF9;&#x5BF9;&#x8C61;B&#x7684;<strong>&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#xFF0C; &#x56E0;&#x6B64;&#xFF0C; &#x5F53;&#x5BF9;&#x8C61;B&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#x65F6;&#xFF0C; &#x5BF9;&#x8C61;B&#x4E5F;&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x5730;&#x91CA;&#x653E;&#x4E86;&#x3002;  </p>
<p>Android&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x4E86;&#x4E09;&#x79CD;&#x7C7B;&#x578B;&#x7684;C++&#x667A;&#x80FD;&#x6307;&#x9488;&#xFF0C; &#x5206;&#x522B;&#x4E3A;<strong>&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</strong>(Light Pointer)&#x3001; <strong>&#x5F3A;&#x6307;&#x9488;</strong>(StrongPointer)&#x548C;<strong>&#x5F31;&#x6307;&#x9488;</strong>(Weak Pointer)&#xFF0C; &#x5176;&#x4E2D;&#xFF0C; &#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;&#x4F7F;&#x7528;&#x4E86;&#x7B80;&#x5355;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6280;&#x672F;&#xFF0C; &#x800C;&#x5F3A;&#x6307;&#x9488;&#x548C;&#x5F31;&#x6307;&#x9488;&#x4F7F;&#x7528;&#x4E86;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6280;&#x672F;  </p>
<p><img src="https://gitee.com/cpu_code/picture_bed/raw/master//20200712145507.png" alt="image-20200712145507418" style="zoom: 67%;"></p>
<p>Android&#x7CFB;&#x7EDF;&#x5C06;<strong>&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5668;</strong>&#x5B9A;&#x4E49;&#x4E3A;&#x4E00;&#x4E2A;<strong>&#x516C;&#x5171;&#x7C7B;</strong>&#xFF0C; &#x6240;&#x6709;&#x652F;&#x6301;&#x4F7F;&#x7528;&#x667A;&#x80FD;&#x6307;&#x9488;&#x7684;&#x5BF9;&#x8C61;&#x7C7B;&#x90FD;&#x5FC5;&#x987B;&#x8981;&#x4ECE;&#x8FD9;&#x4E2A;<strong>&#x516C;&#x5171;&#x7C7B;&#x7EE7;&#x627F;</strong>&#x4E0B;&#x6765;&#x3002; &#x8FD9;&#x6837;&#xFF0C; Android&#x7CFB;&#x7EDF;&#x7684;&#x667A;&#x80FD;&#x6307;&#x9488;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5668;&#x6765;<strong>&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;</strong>&#x4E86;  </p>
<hr>
<h2 id="&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;">&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</h2>
<p><strong>&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</strong>&#x901A;&#x8FC7;<strong>&#x7B80;&#x5355;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</strong>&#x6280;&#x672F;&#x6765;&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x3002;&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x652F;&#x6301;&#x4F7F;&#x7528;<strong>&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</strong>&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x5C31;&#x5FC5;&#x987B;&#x8981;&#x4ECE;<code>LightRefBase</code>&#x7C7B;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#xFF0C;&#x56E0;&#x4E3A;<code>LightRefBase</code>&#x7C7B;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5668;&#x3002;</p>
<h3 id="&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x5206;&#x6790;">&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x5206;&#x6790;</h3>
<p>&#x5206;&#x6790;<code>LightRefBase</code>&#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;</p>
<pre><code class="lang-c++">// frameworks\base\include\utils\RefBase.h

// &#x6A21;&#x677F;&#x7C7B;
// T &#x8868;&#x793A;&#x5BF9;&#x8C61;&#x7684;&#x5B9E;&#x9645;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x5FC5;&#x987B;&#x662F;&#x7EE7;&#x627F;&#x4E86;LightRefBase&#x7C7B;&#x7684;
template &lt;class T&gt;
class LightRefBase
{
public:
    inline LightRefBase() : mCount(0) { }

    // &#x589E;&#x52A0;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;
    inline void incStrong(const void* id) const 
    {
        android_atomic_inc(&amp;mCount);
    }

    //&#x51CF;&#x5C11;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;
    inline void decStrong(const void* id) const 
    {
        if (android_atomic_dec(&amp;mCount) == 1) 
        {
            //&#x91CA;&#x653E;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;
            delete static_cast&lt;const T*&gt;(this);
        }
    }
    //! DEBUGGING ONLY: Get current strong ref count.
    inline int32_t getStrongCount() const 
    {
        return mCount;
    }

protected:
    inline ~LightRefBase() { }

private:
    mutable volatile int32_t mCount;    // &#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;
};
</code></pre>
<p><strong>&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</strong>&#x7684;&#x5B9E;&#x73B0;&#x7C7B;&#x4E3A;<code>sp</code>&#xFF0C;&#x5B83;&#x540C;&#x65F6;&#x4E5F;&#x662F;&#x5F3A;&#x6307;&#x9488;&#x7684;&#x5B9E;&#x73B0;&#x7C7B; : </p>
<pre><code class="lang-c++">// frameworks\base\include\utils\RefBase.h

// &#x6A21;&#x5757;&#x7C7B;&#xFF0C;&#x5176;&#x4E2D;&#xFF0C;&#x6A21;&#x677F;&#x53C2;&#x6570;T&#x8868;&#x793A;&#x5BF9;&#x8C61;&#x7684;&#x5B9E;&#x9645;&#x7C7B;&#x578B;&#xFF0C;&#x5B83;&#x4E5F;&#x662F;&#x5FC5;&#x987B;&#x7EE7;&#x627F;&#x4E86;LightRefBase&#x7C7B;&#x7684;
template &lt;typename T&gt;
class sp
{
public:
    // &#x7EF4;&#x62A4;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;
    typedef typename RefBase::weakref_type weakref_type;

    // m_ptr &#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x91CC;&#x9762;&#x521D;&#x59CB;&#x5316;&#x7684;&#xFF0C;&#x6307;&#x5411;&#x5B9E;&#x9645;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;
    inline sp() : m_ptr(0) { }

    sp(T* other);
    sp(const sp&lt;T&gt;&amp; other);
    template&lt;typename U&gt; sp(U* other);
    template&lt;typename U&gt; sp(const sp&lt;U&gt;&amp; other);

    ~sp();

    // Assignment

    sp&amp; operator = (T* other);
    sp&amp; operator = (const sp&lt;T&gt;&amp; other);

    template&lt;typename U&gt; sp&amp; operator = (const sp&lt;U&gt;&amp; other);
    template&lt;typename U&gt; sp&amp; operator = (U* other);

    //! Special optimization for use by ProcessState (and nobody else).
    void force_set(T* other);

    // Reset

    void clear();

    // Accessors

    inline  T&amp;      operator* () const  { return *m_ptr; }
    inline  T*      operator-&gt; () const { return m_ptr;  }
    inline  T*      get() const         { return m_ptr; }

    // Operators

    COMPARE(==)
    COMPARE(!=)
    COMPARE(&gt;)
    COMPARE(&lt;)
    COMPARE(&lt;=)
    COMPARE(&gt;=)

private:    
    template&lt;typename Y&gt; friend class sp;
    template&lt;typename Y&gt; friend class wp;

    // Optimization for wp::promote().
    sp(T* p, weakref_type* refs);

    T*              m_ptr;
};
</code></pre>
<p><code>sp</code>&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6709;&#x4E24;&#x4E2A;&#x7248;&#x672C;&#xFF0C;&#x4E00;&#x4E2A;&#x662F;&#x666E;&#x901A;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x4E00;&#x4E2A;&#x662F;&#x62F7;&#x8D1D;&#x6784;&#x9020;&#x51FD;&#x6570;  :</p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\include\utils\RefBase.h</span>

<span class="hljs-comment">/*
&#x7531;&#x4E8E;&#x6210;&#x5458;&#x53D8;&#x91CF;m_ptr&#x6240;&#x6307;&#x5411;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x4ECE;LightRefBase&#x7C7B;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#x7684;&#xFF0C;
&#x56E0;&#x6B64;&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x8C03;&#x7528;&#x4E86;LightRefBase&#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;incStrong&#x6765;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x3002;
*/</span>

<span class="hljs-comment">// &#x9996;&#x5148;&#x521D;&#x59CB;&#x5316;&#x6210;&#x5458;&#x53D8;&#x91CF;m_ptr</span>
<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
sp&lt;T&gt;::sp(T* other)
    : m_ptr(other)
{
    <span class="hljs-keyword">if</span> (other) 
    {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x5B83;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;incStrong&#x6765;&#x589E;&#x52A0;&#x5B83;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
        other-&gt;incStrong(<span class="hljs-keyword">this</span>);
    }
}

<span class="hljs-comment">// &#x9996;&#x5148;&#x521D;&#x59CB;&#x5316;&#x6210;&#x5458;&#x53D8;&#x91CF;m_ptr</span>
<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
sp&lt;T&gt;::sp(<span class="hljs-keyword">const</span> sp&lt;T&gt;&amp; other)
    : m_ptr(other.m_ptr)
{
    <span class="hljs-keyword">if</span> (m_ptr) 
    {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x5B83;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;incStrong&#x6765;&#x589E;&#x52A0;&#x5B83;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
        m_ptr-&gt;incStrong(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<p>sp&#x7C7B;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;  :</p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\include\utils\RefBase.h</span>

<span class="hljs-comment">/*
&#x6790;&#x6784;&#x51FD;&#x6570;&#x6267;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#x521A;&#x597D;&#x4E0E;&#x6784;&#x9020;&#x51FD;&#x6570;&#x76F8;&#x53CD;&#xFF0C;&#x5373;&#x8C03;&#x7528;&#x6210;&#x5458;&#x53D8;&#x91CF;m_ptr&#x6240;&#x6307;&#x5411;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;decStrong&#x6765;&#x51CF;&#x5C11;&#x5B83;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#xFF0C;
&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x8C03;&#x7528;LightRefBase&#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;decStrong&#x6765;&#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;
*/</span>

<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
sp&lt;T&gt;::~sp()
{
    <span class="hljs-keyword">if</span> (m_ptr) 
    {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x6210;&#x5458;&#x51FD;&#x6570;decStrong&#x6765;&#x51CF;&#x5C11;&#x5B83;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
        m_ptr-&gt;decStrong(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<h3 id="&#x5E94;&#x7528;&#x5B9E;&#x4F8B;&#x5206;&#x6790;">&#x5E94;&#x7528;&#x5B9E;&#x4F8B;&#x5206;&#x6790;</h3>
<p>&#x5728;<code>external</code>&#x76EE;&#x5F55;&#x4E2D;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;<code>C++</code>&#x5E94;&#x7528;&#x7A0B;&#x5E8F;<code>lightpointer</code>&#x6765;&#x8BF4;&#x660E;<strong>&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</strong>&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#xFF0C;&#x5B83;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-shell">~/Android
    external
        lightpointer
            lightpointer.cpp
            Android.mk
</code></pre>
<p>&#x5E94;&#x7528;&#x7A0B;&#x5E8F;<code>lightpointer</code>&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5B83;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x6E90;&#x6587;&#x4EF6;<code>lightpointer.cpp</code>&#x548C;&#x4E00;&#x4E2A;&#x7F16;&#x8BD1;&#x811A;&#x672C;&#x6587;&#x4EF6;<code>Android.mk</code>  </p>
<p><code>lightpointer.cpp</code>  :</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;utils/RefBase.h&gt;</span></span>

<span class="hljs-keyword">using</span>  <span class="hljs-keyword">namespace</span> android;

<span class="hljs-comment">// &#x7EE7;&#x627F;&#x4E86;LightRefBase&#x7C7B;&#x7684;LightClass&#x7C7B; , &#x80FD;&#x591F;&#x7ED3;&#x5408;&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;&#x6765;&#x4F7F;&#x7528;</span>
<span class="hljs-keyword">class</span> LightClass : <span class="hljs-keyword">public</span> LightRefBase&lt;LightClass&gt;
{
<span class="hljs-keyword">public</span>:
    LightClass()
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Construct LightClass Object.\n&quot;</span>);
    }

    <span class="hljs-keyword">virtual</span> ~LightClass()
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Destory LightClass Object.\n&quot;</span>);
    }

};

<span class="hljs-comment">// lightpointer &#x7684;&#x5165;&#x53E3;&#x51FD;&#x6570; main</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span>** argv)</span>
</span>{
    <span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A; LightClass &#x5BF9;&#x8C61; pLightClass</span>
    LightClass* pLightClass = <span class="hljs-keyword">new</span> LightClass();

    <span class="hljs-comment">// &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488; lpOut &#x6765;&#x5F15;&#x7528;&#x5B83;</span>
    <span class="hljs-comment">// &#x521B;&#x5EFA;&#x8FC7;&#x7A0B;&#x4E2D;, &#x8C03;&#x7528; sp &#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x589E;&#x52A0; LightClass &#x5BF9;&#x8C61; pLightClass &#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#xFF0C;</span>
    <span class="hljs-comment">// &#x5373;&#x6B64;&#x65F6; LightClass &#x5BF9;&#x8C61; pLightClass &#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#x4E3A; 1</span>
    sp&lt;LightClass&gt; lpOut = pLightClass;

    <span class="hljs-comment">// &#x6253;&#x5370; == 1</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Light Ref Count: %d.\n&quot;</span>, pLightClass-&gt;getStrongCount());

    <span class="hljs-comment">//&#x5185;&#x5D4C;&#x7684;&#x4F5C;&#x7528;&#x57DF;</span>
    {
        <span class="hljs-comment">/* 
         * &#x521B;&#x5EFA;&#x4E86;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488; lpInner &#x6765;&#x5F15;&#x7528; LightClass &#x5BF9;&#x8C61; pLightClass
        * LightClass &#x5BF9;&#x8C61; pLightClass &#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#x4F1A;&#x518D;&#x589E;&#x52A0;1
        */</span>
        sp&lt;LightClass&gt; lpInner = lpOut;

        <span class="hljs-comment">// printf&#x8BED;&#x53E5;&#x6253;&#x5370;&#x51FA;&#x6765;&#x7684;&#x6570;&#x5B57;&#x5C31;&#x5E94;&#x8BE5;&#x7B49;&#x4E8E;2</span>
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Light Ref Count: %d.\n&quot;</span>, pLightClass-&gt;getStrongCount());
    }
    <span class="hljs-comment">/* 
     * &#x5F53;&#x5E94;&#x7528;&#x7A0B;&#x5E8F; lightpointer &#x8DF3;&#x51FA;&#x4E86;&#x4F5C;&#x7528;&#x57DF;&#x4E4B;&#x540E;&#xFF0C;&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488; lpInner &#x5C31;&#x88AB;&#x6790;&#x6784;&#x4E86;&#xFF0C;
     *&#x8FD9;&#x65F6;&#x5019; LightClass &#x5BF9;&#x8C61; pLightClass &#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#x5C31;&#x4F1A;&#x51CF;&#x5C11; 1
     */</span>

    <span class="hljs-comment">//&#x6253;&#x5370; == 1</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;Light Ref Count: %d.\n&quot;</span>, pLightClass-&gt;getStrongCount());

    <span class="hljs-comment">/*
     *&#x5F53;&#x5E94;&#x7528;&#x7A0B;&#x5E8F; lightpointer &#x6267;&#x884C;&#x5B8C;&#x6210;&#x4E4B;&#x540E;&#xFF0C;&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488; lpOut &#x4E5F;&#x4F1A;&#x88AB;&#x6790;&#x6784;&#xFF0C;
     *&#x8FD9;&#x65F6; LightClass &#x5BF9;&#x8C61; pLightClass &#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#x5C31;&#x4F1A;&#x518D;&#x6B21;&#x51CF;&#x5C11; 1&#xFF0C;== 0&#xFF0C;&#x4E8E;&#x662F;LightClass&#x5BF9;&#x8C61;pLightClass&#x5C31;&#x4F1A;&#x88AB;&#x91CA;&#x653E;
     */</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Android.mk  </p>
<p>&#x5E94;&#x7528;&#x7A0B;&#x5E8F;<code>lightpointer</code>&#x7684;&#x7F16;&#x8BD1;&#x811A;&#x672C;&#x6587;&#x4EF6;&#xFF0C;&#x5B83;&#x5F15;&#x7528;&#x4E86;<code>libcutils</code>&#x548C; <code>libutils</code>&#x4E24;&#x4E2A;&#x5E93;  </p>
<pre><code class="lang-makefile">LOCAL_PATH := <span class="hljs-variable">$(call my-dir)</span>
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := lightpointer
LOCAL_SRC_FILES := lightpointer.cpp
LOCAL_SHARED_LIBRARIES := \
    libcutils \
    libutils
include $(BUILD_EXECUTABLE)
</code></pre>
<p>&#x5BF9;&#x8FD9;&#x4E2A;C++&#x5DE5;&#x7A0B;&#x8FDB;&#x884C;<strong>&#x7F16;&#x8BD1;</strong>&#x548C;<strong>&#x6253;&#x5305;</strong>  </p>
<pre><code class="lang-shell">mmm ./external/lightpointer/
make snod
</code></pre>
<p>&#x7F16;&#x8BD1;&#x6210;&#x529F;&#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5728; <code>out/target/product/gerneric/system/bin</code> &#x76EE;&#x5F55;&#x4E0B;&#x770B;&#x5230;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6587;&#x4EF6;<code>lightpointer</code>&#x4E86;&#xFF1B;</p>
<p>&#x6253;&#x5305;&#x6210;&#x529F;&#x4E4B;&#x540E;&#xFF0C;&#x8BE5;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5C31;&#x5305;&#x542B;&#x5728;<code>out/target/product/gerneric</code>&#x76EE;&#x5F55;&#x4E0B;&#x7684;<code>Android</code>&#x7CFB;&#x7EDF;&#x955C;&#x50CF;&#x6587;&#x4EF6;<code>system.img</code>&#x4E2D;&#x4E86;&#x3002;  </p>
<pre><code class="lang-shell"># &#x4F7F;&#x7528;&#x65B0;&#x5F97;&#x5230;&#x7684;&#x7CFB;&#x7EDF;&#x955C;&#x50CF;&#x6587;&#x4EF6; system.img &#x6765;&#x542F;&#x52A8; Android &#x6A21;&#x62DF;&#x5668;
emulator &amp;

# &#x4F7F;&#x7528;adb&#x5DE5;&#x5177;&#x8FDE;&#x63A5;&#x4E0A;&#x5B83;
adb shell

# &#x8FDB;&#x5165;&#x5230;/system/bin &#x76EE;&#x5F55;&#x4E2D;
cd system/bin/

# &#x8FD0;&#x884C;&#x5E94;&#x7528;&#x7A0B;&#x5E8F; lightpointer &#x6765;&#x67E5;&#x770B;&#x5B83;&#x7684;&#x8F93;&#x51FA;
./lightpointer
</code></pre>
<p><img src="https://gitee.com/cpu_code/picture_bed/raw/master//20200712214524.png" alt="image-20200712214524861"></p>
<p>&#x5982;&#x679C;&#x80FD;&#x770B;&#x5230;&#x4E0A;&#x9762;&#x7684;&#x8F93;&#x51FA;&#xFF0C;&#x5C31;&#x8BF4;&#x660E;&#x6211;&#x4EEC;&#x524D;&#x9762;&#x5BF9;<strong>&#x8F7B;&#x91CF;&#x7EA7;&#x6307;&#x9488;</strong>&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x7684;&#x5206;&#x6790;&#x662F;&#x6B63;&#x786E;&#x7684;&#x3002;  </p>
<hr>
<h2 id="&#x5F3A;&#x6307;&#x9488;&#x548C;&#x5F31;&#x6307;&#x9488;">&#x5F3A;&#x6307;&#x9488;&#x548C;&#x5F31;&#x6307;&#x9488;</h2>
<p>&#x5F3A;&#x6307;&#x9488;&#x548C;&#x5F31;&#x6307;&#x9488;&#x901A;&#x8FC7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6765;&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x3002;  </p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x8981;&#x652F;&#x6301;&#x4F7F;&#x7528;<strong>&#x5F3A;&#x6307;&#x9488;</strong>&#x548C;<strong>&#x5F31;&#x6307;&#x9488;</strong>&#xFF0C;&#x90A3;&#x4E48;&#x5B83;&#x5C31;&#x5FC5;&#x987B;&#x4ECE;<code>RefBase</code>&#x7C7B;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#xFF0C;&#x56E0;&#x4E3A;<code>RefBase</code>&#x7C7B;&#x63D0;&#x4F9B;&#x4E86;<strong>&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5668;</strong>&#x548C;<strong>&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5668;</strong>&#x3002;  </p>
<h3 id="&#x5F3A;&#x6307;&#x9488;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x5206;&#x6790;">&#x5F3A;&#x6307;&#x9488;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x5206;&#x6790;</h3>
<p>&#x5206;&#x6790;RefBase&#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;  :</p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\include\utils\RefBase.h</span>

<span class="hljs-keyword">class</span> RefBase
{
<span class="hljs-keyword">public</span>:
    <span class="hljs-comment">// &#x6210;&#x5458;&#x51FD;&#x6570; incStrong &#x548C; decStrong &#x6765;&#x7EF4;&#x62A4;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">incStrong</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span> <span class="hljs-keyword">const</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">decStrong</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span> <span class="hljs-keyword">const</span></span>;

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">forceIncStrong</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span> <span class="hljs-keyword">const</span></span>;

    <span class="hljs-comment">//! DEBUGGING ONLY: Get current strong ref count.</span>
    <span class="hljs-keyword">int32_t</span>    getStrongCount() <span class="hljs-keyword">const</span>;

    <span class="hljs-keyword">class</span> weakref_type
    {
    <span class="hljs-keyword">public</span>:
        <span class="hljs-function">RefBase* <span class="hljs-title">refBase</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span></span>;

        <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">incWeak</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;
        <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">decWeak</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;

        <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">attemptIncStrong</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;

        <span class="hljs-comment">//! This is only safe if you have set OBJECT_LIFETIME_FOREVER.</span>
        <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">attemptIncWeak</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;

        <span class="hljs-comment">//! DEBUGGING ONLY: Get current weak ref count.</span>
        <span class="hljs-keyword">int32_t</span>    getWeakCount() <span class="hljs-keyword">const</span>;

        <span class="hljs-comment">//! DEBUGGING ONLY: Print references held on object.</span>
        <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printRefs</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span></span>;

        <span class="hljs-comment">//! DEBUGGING ONLY: Enable tracking for this object.</span>
        <span class="hljs-comment">// enable -- enable/disable tracking</span>
        <span class="hljs-comment">// retain -- when tracking is enable, if true, then we save a stack trace</span>
        <span class="hljs-comment">//           for each reference and dereference; when retain == false, we</span>
        <span class="hljs-comment">//           match up references and dereferences and keep only the </span>
        <span class="hljs-comment">//           outstanding ones.</span>

        <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">trackMe</span><span class="hljs-params">(<span class="hljs-keyword">bool</span> enable, <span class="hljs-keyword">bool</span> retain)</span></span>;
    };

    <span class="hljs-function">weakref_type* <span class="hljs-title">createWeak</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span> <span class="hljs-keyword">const</span></span>;

    <span class="hljs-function">weakref_type* <span class="hljs-title">getWeakRefs</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span></span>;

    <span class="hljs-comment">//! DEBUGGING ONLY: Print references held on object.</span>
    <span class="hljs-function"><span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span>    <span class="hljs-title">printRefs</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span> </span>{ getWeakRefs()-&gt;printRefs(); }

    <span class="hljs-comment">//! DEBUGGING ONLY: Enable tracking of object.</span>
    <span class="hljs-function"><span class="hljs-keyword">inline</span> <span class="hljs-keyword">void</span> <span class="hljs-title">trackMe</span><span class="hljs-params">(<span class="hljs-keyword">bool</span> enable, <span class="hljs-keyword">bool</span> retain)</span>
    </span>{ 
        getWeakRefs()-&gt;trackMe(enable, retain); 
    }

<span class="hljs-keyword">protected</span>:
    RefBase();
    <span class="hljs-keyword">virtual</span> ~RefBase();

    <span class="hljs-comment">//! Flags for extendObjectLifetime()</span>
    <span class="hljs-keyword">enum</span> 
    {
        OBJECT_LIFETIME_WEAK    = <span class="hljs-number">0x0001</span>,
        OBJECT_LIFETIME_FOREVER = <span class="hljs-number">0x0003</span>
    };

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">extendObjectLifetime</span><span class="hljs-params">(int32_t mode)</span></span>;

    <span class="hljs-comment">//! Flags for onIncStrongAttempted()</span>
    <span class="hljs-keyword">enum</span> 
    {
        FIRST_INC_STRONG = <span class="hljs-number">0x0001</span>
    };

    <span class="hljs-function"><span class="hljs-keyword">virtual</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onFirstRef</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">virtual</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLastStrongRef</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">virtual</span> <span class="hljs-keyword">bool</span> <span class="hljs-title">onIncStrongAttempted</span><span class="hljs-params">(uint32_t flags, <span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">virtual</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLastWeakRef</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)</span></span>;

<span class="hljs-keyword">private</span>:
    <span class="hljs-keyword">friend</span> <span class="hljs-keyword">class</span> weakref_type;
    <span class="hljs-keyword">class</span> weakref_impl;

    RefBase(<span class="hljs-keyword">const</span> RefBase&amp; o);
    RefBase&amp; <span class="hljs-keyword">operator</span>=(<span class="hljs-keyword">const</span> RefBase&amp; o);

    <span class="hljs-comment">//&#x4F7F;&#x7528;&#x4E00;&#x4E2A;weakref_impl&#x5BF9;&#x8C61;&#xFF0C;&#x5373;&#x6210;&#x5458;&#x53D8;&#x91CF; mRefs &#x6765;&#x63CF;&#x8FF0;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    weakref_impl* <span class="hljs-keyword">const</span> mRefs;
};
</code></pre>
<p><code>weakref_impl</code>&#x7C7B;&#x540C;&#x65F6;&#x4E3A;&#x5BF9;&#x8C61;&#x63D0;&#x4F9B;&#x4E86;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;  :</p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">/*
weakref_type &#x7C7B;&#x5B9A;&#x4E49;&#x5728;RefBase&#x7C7B;&#x7684;&#x5185;&#x90E8;&#xFF0C;&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x6210;&#x5458;&#x51FD;&#x6570; incWeak&#x3001;decWeak&#x3001;attemptIncStrong &#x548C; attemptIncWeak &#x6765;
&#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570; . 
weakref_type &#x7C7B;&#x53EA;&#x5B9A;&#x4E49;&#x4E86;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x7EF4;&#x62A4;&#x63A5;&#x53E3;&#xFF0C;&#x5177;&#x4F53;&#x7684;&#x5B9E;&#x73B0;&#x662F;&#x7531; weakref_impl &#x7C7B;&#x63D0;&#x4F9B;&#x7684;
*/</span>
<span class="hljs-comment">// weakref_impl&#x7C7B;&#x7EE7;&#x627F;&#x4E86;weakref_type&#x7C7B;</span>
<span class="hljs-keyword">class</span> RefBase::weakref_impl : <span class="hljs-keyword">public</span> RefBase::weakref_type
{
<span class="hljs-keyword">public</span>:
    <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">int32_t</span>    mStrong;    <span class="hljs-comment">// &#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">int32_t</span>    mWeak;        <span class="hljs-comment">// &#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    RefBase* <span class="hljs-keyword">const</span>      mBase;        <span class="hljs-comment">// &#x6307;&#x5411;&#x4E86;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5730;&#x5740;</span>
    <span class="hljs-comment">/*
     * &#x4E00;&#x4E2A;&#x6807;&#x5FD7;&#x503C;&#xFF0C;&#x7528;&#x6765;&#x63CF;&#x8FF0;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x63A7;&#x5236;&#x65B9;&#x5F0F;
     * &#x53D6;&#x503C;&#x8303;&#x56F4;&#x4E3A;0&#x3001;OBJECT_LIFETIME_WEAK &#x6216;&#x8005; OBJECT_LIFETIME_FOREVER
     * 0 : &#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x53EA;&#x53D7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5F71;&#x54CD;
     * OBJECT_LIFETIME_WEAK : &#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x540C;&#x65F6;&#x53D7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5F71;&#x54CD;&#xFF1B;
     * OBJECT_LIFETIME_FOREVER : &#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x5B8C;&#x5168;&#x4E0D;&#x53D7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x6216;&#x8005;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5F71;&#x54CD;
     */</span>
    <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">int32_t</span>    mFlags;        

<span class="hljs-comment">// &#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x6709;&#x8C03;&#x8BD5;&#x548C;&#x975E;&#x8C03;&#x8BD5;&#x4E24;&#x4E2A;&#x7248;&#x672C;</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> !DEBUG_REFS</span>
    <span class="hljs-comment">// &#x88AB;&#x7F16;&#x8BD1;&#x6210;&#x975E;&#x8C03;&#x8BD5;&#x7248;&#x672C;</span>

    <span class="hljs-comment">// &#x6210;&#x5458;&#x53D8;&#x91CF;mStrong&#x7684;&#x503C;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x4E3A; INITIAL_STRONG_VALUE == 1 &lt;&lt; 28</span>
    weakref_impl(RefBase* base)
        : mStrong(INITIAL_STRONG_VALUE)
        , mWeak(<span class="hljs-number">0</span>)
        , mBase(base)
        , mFlags(<span class="hljs-number">0</span>)
    {
    }

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addStrongRef</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* <span class="hljs-comment">/*id*/</span>)</span> </span>{ }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">removeStrongRef</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* <span class="hljs-comment">/*id*/</span>)</span> </span>{ }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">addWeakRef</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* <span class="hljs-comment">/*id*/</span>)</span> </span>{ }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">removeWeakRef</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* <span class="hljs-comment">/*id*/</span>)</span> </span>{ }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">printRefs</span><span class="hljs-params">()</span> <span class="hljs-keyword">const</span> </span>{ }
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">trackMe</span><span class="hljs-params">(<span class="hljs-keyword">bool</span>, <span class="hljs-keyword">bool</span>)</span> </span>{ }

<span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span>
    <span class="hljs-comment">// &#x7C7B;&#x88AB;&#x7F16;&#x8BD1;&#x6210;&#x8C03;&#x8BD5;&#x7248;&#x672C;</span>
    <span class="hljs-comment">//...</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
};
</code></pre>
<p>RefBase&#x3001;weakref_type&#x548C;weakref_impl&#x7C7B;&#x7684;&#x5173;&#x7CFB;  : </p>
<p><img src="https://gitee.com/cpu_code/picture_bed/raw/master//20200713090936.png" alt="RefBase&#x3001;weakref_type&#x548C;weakref_impl&#x7C7B;&#x7684;&#x5173;&#x7CFB;" style="zoom: 67%;"></p>
<p>&#x6BCF;&#x4E00;&#x4E2A;RefBase&#x5BF9;&#x8C61;&#x90FD;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;weakref_impl&#x5BF9;&#x8C61;&#xFF0C; &#x800C;&#x540E;&#x8005;&#x7EE7;&#x627F;&#x4E86;weakref_type&#x7C7B;  </p>
<hr>
<p>&#x5F3A;&#x6307;&#x9488;&#x7684;&#x5B9E;&#x73B0;&#x7C7B;&#x4E3A;sp&#xFF0C;  </p>
<p>sp&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;  : </p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\include\utils\RefBase.h</span>

<span class="hljs-comment">// &#x6A21;&#x5757;&#x53C2;&#x6570; T &#x662F;&#x4E00;&#x4E2A;&#x7EE7;&#x627F;&#x4E86; RefBase &#x7C7B;&#x7684;&#x5B50;&#x7C7B;</span>
<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
sp&lt;T&gt;::sp(T* other)
    : m_ptr(other)
{
    <span class="hljs-comment">// &#x8C03;&#x7528;&#x4E86; RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; incStrong &#x6765;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    <span class="hljs-keyword">if</span> (other) 
    {
        other-&gt;incStrong(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; incStrong&#x5B9E;&#x73B0;</span>
<span class="hljs-keyword">void</span> RefBase::incStrong(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id) <span class="hljs-keyword">const</span>
{
    weakref_impl* <span class="hljs-keyword">const</span> refs = mRefs;
    refs-&gt;addWeakRef(id);

    <span class="hljs-comment">// &#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    refs-&gt;incWeak(id);
    refs-&gt;addStrongRef(id);

    <span class="hljs-comment">// &#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570; , &#x589E;&#x52A0;RefBase&#x7C7B;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5BF9;&#x8C61;mRefs&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;mStrong&#x7684;&#x503C;</span>
    <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5BF9;&#x8C61;&#x539F;&#x6765;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#xFF0C; &#x5373;&#x52A0;1&#x524D;&#x7684;&#x503C;</span>
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int32_t</span> c = android_atomic_inc(&amp;refs-&gt;mStrong);
    LOG_ASSERT(c &gt; <span class="hljs-number">0</span>, <span class="hljs-string">&quot;incStrong() called on %p after last strong ref&quot;</span>, refs);
<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> PRINT_REFS</span>
    LOGD(<span class="hljs-string">&quot;incStrong of %p from %p: cnt=%d\n&quot;</span>, <span class="hljs-keyword">this</span>, id, c);
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
    <span class="hljs-keyword">if</span> (c != INITIAL_STRONG_VALUE)  
    {
        <span class="hljs-keyword">return</span>;
    }

    <span class="hljs-comment">// &#x5C06;RefBase&#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;incStrong&#x9700;&#x8981;&#x5C06;&#x5B83;&#x8C03;&#x6574;&#x4E3A;1</span>
    android_atomic_add(-INITIAL_STRONG_VALUE, &amp;refs-&gt;mStrong);
    <span class="hljs-comment">/*
     *&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; onFirstRef &#x6765;&#x901A;&#x77E5;&#x5BF9;&#x8C61;&#xFF0C; &#x5B83;&#x88AB;&#x5F3A;&#x6307;&#x9488;&#x5F15;&#x7528;&#x4E86;
     * RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; onFirstRef &#x662F;&#x4E00;&#x4E2A;&#x7A7A;&#x5B9E;&#x73B0;&#xFF0C;
     * &#x5982;&#x679C;&#x5B50;&#x7C7B;&#x60F3;&#x8981;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x4E8B;&#x4EF6;&#xFF0C; &#x90A3;&#x4E48;&#x5C31;&#x5FC5;&#x987B;&#x8981;&#x91CD;&#x5199;&#x6210;&#x5458;&#x51FD;&#x6570; onFirstRef
     */</span>
    <span class="hljs-keyword">const_cast</span>&lt;RefBase*&gt;(<span class="hljs-keyword">this</span>)-&gt;onFirstRef();
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> INITIAL_STRONG_VALUE (1&lt;&lt;28)</span>
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// RefBase&#x7C7B;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;mRefs&#x662F;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x521D;&#x59CB;&#x5316;</span>
RefBase::RefBase()
    : mRefs(<span class="hljs-keyword">new</span> weakref_impl(<span class="hljs-keyword">this</span>))
{
<span class="hljs-comment">//    LOGV(&quot;Creating refs %p with RefBase %p\n&quot;, mRefs, this);</span>
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// &#x8C03;&#x7528; weakref_type &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; incWeak &#x6765;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
<span class="hljs-keyword">void</span> RefBase::weakref_type::incWeak(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)
{
    <span class="hljs-comment">// this&#x6307;&#x9488;&#x6307;&#x5411;&#x7684;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x4E00;&#x4E2A;weakref_impl&#x5BF9;&#x8C61;</span>
    <span class="hljs-comment">// &#x5C06;&#x5B83;&#x8F6C;&#x6362;&#x4E3A;&#x4E00;&#x4E2A;weakref_impl&#x6307;&#x9488; impl</span>
    weakref_impl* <span class="hljs-keyword">const</span> impl = <span class="hljs-keyword">static_cast</span>&lt;weakref_impl*&gt;(<span class="hljs-keyword">this</span>);
    impl-&gt;addWeakRef(id);

    <span class="hljs-comment">// &#x589E;&#x52A0;&#x5B83;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;mWeak&#x7684;&#x503C;&#xFF0C; &#x5373;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int32_t</span> c = android_atomic_inc(&amp;impl-&gt;mWeak);
    <span class="hljs-comment">// &#x8C03;&#x7528;&#x662F;&#x4E0E;&#x8C03;&#x8BD5;&#x76F8;&#x5173;&#x7684;</span>
    LOG_ASSERT(c &gt;= <span class="hljs-number">0</span>, <span class="hljs-string">&quot;incWeak called on %p after last weak ref&quot;</span>, <span class="hljs-keyword">this</span>);
}
</code></pre>
<p>&#x5F3A;&#x6307;&#x9488;&#x7C7B;<code>sp</code>&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0; , &#x4E3B;&#x8981;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x5C31;&#x662F;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5F3A;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x548C;<strong>&#x5F31;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x3002; &#x4ECE;&#x8FD9;&#x91CC;&#x5C31;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C; &#x867D;&#x7136;&#x6211;&#x4EEC;&#x7684;&#x76EE;&#x7684;&#x662F;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5F3A;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x540C;&#x65F6;&#x4E5F;&#x4F1A;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5F31;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#xFF0C; &#x5373;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5F31;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x4E00;&#x5B9A;&#x662F;<strong>&#x5927;&#x4E8E;</strong>&#x6216; <strong>&#x7B49;&#x4E8E;</strong>&#x5B83;&#x7684;<strong>&#x5F3A;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x7684;&#x3002;  </p>
<hr>
<p>sp&#x7C7B;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;  :</p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\include\utils\RefBase.h</span>

<span class="hljs-comment">// sp&#x7C7B;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;m_ptr&#x6240;&#x6307;&#x5411;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x7EE7;&#x627F;&#x4E86;RefBase&#x7C7B;</span>
<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
sp&lt;T&gt;::~sp()
{
    <span class="hljs-keyword">if</span> (m_ptr) 
    {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x4E86;RefBase&#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570;decStrong&#x6765;&#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
        m_ptr-&gt;decStrong(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// &#x6790;&#x6784;&#x51FD;&#x6570; , &#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
<span class="hljs-keyword">void</span> RefBase::decStrong(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id) <span class="hljs-keyword">const</span>
{
    weakref_impl* <span class="hljs-keyword">const</span> refs = mRefs;

    <span class="hljs-comment">// &#x975E;&#x8C03;&#x8BD5;&#x7248;&#x672C;&#x4E2D;&#x662F;&#x7A7A;&#x51FD;&#x6570;&#x8C03;&#x7528;</span>
    refs-&gt;removeStrongRef(id);

    <span class="hljs-comment">// &#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5BF9;&#x8C61;&#x539F;&#x6765;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#xFF0C;&#x5373;&#x51CF;1&#x524D;&#x7684;&#x503C;&#xFF0C; &#x4FDD;&#x5B58;&#x5728;&#x53D8;&#x91CF;c&#x4E2D;</span>
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int32_t</span> c = android_atomic_dec(&amp;refs-&gt;mStrong);
<span class="hljs-meta">#<span class="hljs-meta-keyword">if</span> PRINT_REFS</span>
    LOGD(<span class="hljs-string">&quot;decStrong of %p from %p: cnt=%d\n&quot;</span>, <span class="hljs-keyword">this</span>, id, c);
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
    LOG_ASSERT(c &gt;= <span class="hljs-number">1</span>, <span class="hljs-string">&quot;decStrong() called on %p too many times&quot;</span>, refs);

    <span class="hljs-comment">// &#x5982;&#x679C;&#x53D8;&#x91CF;c&#x7684;&#x503C;&#x7B49;&#x4E8E;1&#xFF0C; &#x5C31;&#x8BF4;&#x660E;&#x6B64;&#x65F6;&#x518D;&#x4E5F;&#x6CA1;&#x6709;&#x5F3A;&#x6307;&#x9488;&#x5F15;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4E86;</span>
    <span class="hljs-keyword">if</span> (c == <span class="hljs-number">1</span>) 
    {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; onLastStrongRef &#x6267;&#x884C;&#x4E00;&#x4E9B;&#x4E1A;&#x52A1;&#x76F8;&#x5173;&#x7684;&#x903B;&#x8F91;</span>
        <span class="hljs-keyword">const_cast</span>&lt;RefBase*&gt;(<span class="hljs-keyword">this</span>)-&gt;onLastStrongRef(id);

        <span class="hljs-comment">// &#x8003;&#x8651;&#x662F;&#x5426;&#x9700;&#x8981;&#x91CA;&#x653E;&#x8BE5;&#x5BF9;&#x8C61;</span>
        <span class="hljs-comment">// &#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x662F;&#x5426;&#x53D7;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x63A7;&#x5236;&#xFF0C; </span>
        <span class="hljs-comment">//&#x5373; RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF; mRefs &#x7684;&#x6807;&#x5FD7;&#x503C; mFlags &#x7684; OBJECT_LIFETIME_WEAK &#x4F4D;&#x662F;&#x5426;&#x7B49;&#x4E8E;1</span>
        <span class="hljs-comment">// &#x4E0D;&#x7B49;&#x4E8E;1&#xFF0C; &#x5C31;&#x8BF4;&#x660E;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x4E0D;&#x53D7;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5F71;&#x54CD;</span>
        <span class="hljs-keyword">if</span>( (refs-&gt;mFlags &amp; OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) 
        {
            <span class="hljs-comment">// &#x91CA;&#x653E;&#x5BF9;&#x8C61;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;&#xFF0C; &#x8FD9;&#x540C;&#x65F6;&#x4F1A;&#x5BFC;&#x81F4;RefBase&#x7C7B;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;</span>
            <span class="hljs-keyword">delete</span> <span class="hljs-keyword">this</span>;
        }
    }

    <span class="hljs-comment">// &#x975E;&#x8C03;&#x8BD5;&#x7248;&#x672C;&#x4E2D;&#x662F;&#x7A7A;&#x51FD;&#x6570;&#x8C03;&#x7528;</span>
    refs-&gt;removeWeakRef(id);

    <span class="hljs-comment">/*
     * &#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x7684;&#x64CD;&#x4F5C;
     * &#x53D8;&#x91CF; refs &#x6307;&#x5411;&#x7684;&#x662F; RefBase &#x7C7B;&#x5185;&#x90E8;&#x7684; weakref_impl &#x5BF9;&#x8C61; mRefs&#x3002; 
     * weakref_impl &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; decWeak &#x662F;&#x4ECE;&#x7236;&#x7C7B; weakref_type &#x7EE7;&#x627F;&#x4E0B;&#x6765;&#x7684;&#xFF0C; 
     * &#x56E0;&#x6B64;&#xFF0C; &#x63A5;&#x4E0B;&#x6765;&#x5B9E;&#x9645;&#x6267;&#x884C;&#x7684;&#x662F; weakref_type &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; decWeak
     */</span>
    refs-&gt;decWeak(id);
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// RefBase&#x7C7B;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;</span>
RefBase::~RefBase()
{
<span class="hljs-comment">//    LOGV(&quot;Destroying RefBase %p (refs %p)\n&quot;, this, mRefs);</span>

    <span class="hljs-comment">/*
     * &#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C; == 0
     * RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF; mRefs &#x6307;&#x5411;&#x7684;&#x662F;&#x4E00;&#x4E2A; weakref_impl &#x5BF9;&#x8C61;&#xFF0C; &#x5B83;&#x662F;&#x5728; RefBase &#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x521B;&#x5EFA;&#x7684;&#x3002; 
     * &#x5B83;&#x6240;&#x5C5E;&#x7684;RefBase&#x5BF9;&#x8C61;&#x5DF2;&#x7ECF;&#x4E0D;&#x5B58;&#x5728;&#x4E86;&#xFF0C;&#x800C;&#x4E14;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C; == 0&#xFF0C; &#x5B83;&#x4E5F;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x5B58;&#x5728;&#x4E86;
    */</span>
    <span class="hljs-keyword">if</span> (mRefs-&gt;mWeak == <span class="hljs-number">0</span>) 
    {
<span class="hljs-comment">//        LOGV(&quot;Freeing refs %p of old RefBase %p\n&quot;, mRefs, this);</span>

        <span class="hljs-comment">// &#x628A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5BF9;&#x8C61;mRefs&#x4E5F;&#x4E00;&#x8D77;&#x91CA;&#x653E;</span>
        <span class="hljs-keyword">delete</span> mRefs;
    }
    <span class="hljs-comment">/*
     * &#x5728;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C; &gt; 0&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C; &#x6211;&#x4EEC;&#x53EA;&#x80FD;&#x5C06;&#x5BF9;&#x5E94;&#x7684; RefBase &#x5BF9;&#x8C61;&#x91CA;&#x653E;&#x6389;&#xFF0C; 
     * &#x800C;&#x4E0D;&#x80FD;&#x5C06;&#x8BE5; RefBase &#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x7684; weakref_impl &#x5BF9;&#x8C61;&#x4E5F;&#x91CA;&#x653E;&#x6389;
    */</span> 
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// weakref_type &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; decWeak</span>
<span class="hljs-keyword">void</span> RefBase::weakref_type::decWeak(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id)
{
    weakref_impl* <span class="hljs-keyword">const</span> impl = <span class="hljs-keyword">static_cast</span>&lt;weakref_impl*&gt;(<span class="hljs-keyword">this</span>);

    <span class="hljs-comment">// &#x975E;&#x8C03;&#x8BD5;&#x7248;&#x672C;&#x4E2D;&#x662F;&#x7A7A;&#x51FD;&#x6570;&#x8C03;&#x7528;</span>
    impl-&gt;removeWeakRef(id);

    <span class="hljs-comment">// &#x51CF;&#x5C11;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#xFF0C; &#x5E76;&#x4E14;&#x8FD4;&#x56DE;&#x51CF;&#x5C11;&#x4E4B;&#x524D;&#x7684;&#x503C;&#xFF0C; &#x4FDD;&#x5B58;&#x5728;&#x53D8;&#x91CF;c&#x4E2D;</span>
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int32_t</span> c = android_atomic_dec(&amp;impl-&gt;mWeak);

    LOG_ASSERT(c &gt;= <span class="hljs-number">1</span>, <span class="hljs-string">&quot;decWeak called on %p too many times&quot;</span>, <span class="hljs-keyword">this</span>);

    <span class="hljs-keyword">if</span> (c != <span class="hljs-number">1</span>) 
    {
        <span class="hljs-comment">// &#x8BF4;&#x660E;&#x8FD8;&#x6709;&#x5176;&#x4ED6;&#x7684;&#x5F31;&#x6307;&#x9488;&#x5728;&#x5F15;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x5C31;&#x4E0D;&#x7528;&#x8FDB;&#x4E00;&#x6B65;&#x5904;&#x7406;&#x4E86;</span>
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-comment">// c == 1&#xFF0C; &#x90A3;&#x4E48;&#x5C31;&#x8BF4;&#x660E;&#x518D;&#x4E5F;&#x6CA1;&#x6709;&#x5F31;&#x6307;&#x9488;&#x5F15;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4E86;&#xFF0C; &#x540C;&#x65F6;&#x4E5F;&#x8BF4;&#x660E;&#x6CA1;&#x6709;&#x5F3A;&#x6307;&#x9488;&#x5F15;&#x7528;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;</span>
    <span class="hljs-comment">// &#x5F53;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C; == 0 &#x65F6;&#xFF0C; &#x5B83;&#x7684;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C;&#x4E5F;&#x4E00;&#x5B9A; == 0</span>

    <span class="hljs-comment">// &#x5728;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C; == 0 &#x65F6;&#xFF0C; &#x6211;&#x4EEC;&#x5C31;&#x8981;&#x8003;&#x8651;&#x662F;&#x5426;&#x9700;&#x8981;&#x5C06;&#x8BE5;&#x5BF9;&#x8C61;&#x91CA;&#x653E;&#x6389;&#x3002; </span>
    <span class="hljs-comment">// &#x8FD9;&#x53D6;&#x51B3;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x63A7;&#x5236;&#x65B9;&#x5F0F;&#xFF0C; &#x4EE5;&#x53CA;&#x8BE5;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x88AB;&#x5F3A;&#x6307;&#x9488;&#x5F15;&#x7528;&#x8FC7;&#x3002;</span>

    <span class="hljs-keyword">if</span> ((impl-&gt;mFlags &amp; OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) 
    {
        <span class="hljs-comment">// &#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x53EA;&#x53D7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x63A7;&#x5236;</span>


        <span class="hljs-keyword">if</span> (impl-&gt;mStrong == INITIAL_STRONG_VALUE)
        {
            <span class="hljs-comment">// &#x5BF9;&#x8C61;&#x4ECE;&#x6765;&#x6CA1;&#x6709;&#x88AB;&#x5F3A;&#x6307;&#x9488;&#x5F15;&#x7528;&#x8FC7;&#xFF0C; &#x90A3;&#x4E48;&#x5728;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x503C; == 0&#x65F6;</span>
            <span class="hljs-comment">// &#x5C06;&#x8BE5;&#x5BF9;&#x8C61;&#x91CA;&#x653E;&#x6389;</span>
            <span class="hljs-keyword">delete</span> impl-&gt;mBase;
        }
        <span class="hljs-keyword">else</span> 
        {
<span class="hljs-comment">//            LOGV(&quot;Freeing refs %p of old RefBase %p\n&quot;, this, impl-&gt;mBase);</span>
            <span class="hljs-comment">// &#x53EA;&#x91CA;&#x653E;&#x5176;&#x5185;&#x90E8;&#x7684;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x5668;&#x5BF9;&#x8C61; weakref_impl</span>
            <span class="hljs-keyword">delete</span> impl;
        }
    } 
    <span class="hljs-keyword">else</span> 
    {
        <span class="hljs-comment">// &#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x53D7;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x63A7;&#x5236;</span>

        <span class="hljs-comment">// &#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; onLastWeakRef &#x6267;&#x884C;&#x4E00;&#x4E9B;&#x4E1A;&#x52A1;&#x76F8;&#x5173;&#x7684;&#x903B;&#x8F91;</span>
        impl-&gt;mBase-&gt;onLastWeakRef(id);

        <span class="hljs-comment">/*
         * &#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x662F;&#x5426;&#x5B8C;&#x5168;&#x4E0D;&#x53D7;&#x5F3A;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x548C;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;&#x63A7;&#x5236;&#xFF0C; 
         * &#x5373; RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF; mRefs &#x7684;&#x6807;&#x5FD7;&#x503C; mFlags &#x7684;OBJECT_LIFETIME_FOREVER&#x4F4D;&#x662F;&#x5426; == 1
         */</span>
        <span class="hljs-keyword">if</span> ((impl-&gt;mFlags &amp; OBJECT_LIFETIME_FOREVER) != OBJECT_LIFETIME_FOREVER) 
        {
            <span class="hljs-comment">// != 1&#xFF0C;&#x91CA;&#x653E;&#x5BF9;&#x8C61;&#x6240;&#x5360;&#x7528;&#x7684;&#x5185;&#x5B58;</span>
            <span class="hljs-keyword">delete</span> impl-&gt;mBase;
        }
    }
}
</code></pre>
<p>&#x5BF9;&#x5BF9;&#x8C61;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x63A7;&#x5236;&#x65B9;&#x5F0F;&#x4F5C;&#x4E00;&#x4E2A;&#x5C0F;&#x7ED3;  : </p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<strong>&#x751F;&#x547D;&#x5468;&#x671F;&#x63A7;&#x5236;&#x6807;&#x5FD7;&#x503C;</strong> ==  <code>0</code>&#xFF0C; &#x90A3;&#x4E48;&#x53EA;&#x8981;&#x5B83;&#x7684;<strong>&#x5F3A;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x503C; == <code>0</code>&#xFF0C; &#x7CFB;&#x7EDF;&#x5C31;&#x4F1A;&#x81EA;&#x52A8;<strong>&#x91CA;&#x653E;</strong>&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;  </p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<strong>&#x751F;&#x547D;&#x5468;&#x671F;&#x63A7;&#x5236;&#x6807;&#x5FD7;&#x503C;</strong> ==  <code>OBJECT_LIFETIME_WEAK</code>&#xFF0C; &#x90A3;&#x4E48;&#x53EA;&#x6709;&#x5F53;&#x5B83;&#x7684;<strong>&#x5F3A;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x503C; &#x548C; <strong>&#x5F31;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x503C;&#x90FD; == 0&#xFF0C; &#x7CFB;&#x7EDF;&#x624D;&#x4F1A;&#x81EA;&#x52A8;<strong>&#x91CA;&#x653E;</strong>&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;<strong>&#x751F;&#x547D;&#x5468;&#x671F;&#x63A7;&#x5236;&#x6807;&#x5FD7;&#x503C;</strong>&#x88AB;&#x8BBE;&#x7F6E;&#x4E3A; <code>OBJECT_LIFETIME_FOREVER</code>&#xFF0C; &#x90A3;&#x4E48;&#x7CFB;&#x7EDF;&#x5C31;<strong>&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x91CA;&#x653E;</strong>&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C; &#x5B83;&#x9700;&#x8981;&#x7531;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x6765;<strong>&#x624B;&#x52A8;&#x5730;&#x91CA;&#x653E;</strong>&#x3002;  </p>
<hr>
<h3 id="&#x5F31;&#x6307;&#x9488;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x5206;&#x6790;">&#x5F31;&#x6307;&#x9488;&#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#x5206;&#x6790;</h3>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x652F;&#x6301;&#x4F7F;&#x7528;<strong>&#x5F31;&#x6307;&#x9488;</strong>&#xFF0C; &#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x7C7B;&#x5C31;&#x5FC5;&#x987B;&#x8981;&#x4ECE;<code>RefBase</code>&#x7C7B;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#xFF0C; &#x56E0;&#x4E3A;<code>RefBase</code>&#x7C7B;&#x63D0;&#x4F9B;&#x4E86;<strong>&#x5F31;&#x5F15;&#x7528;</strong>&#x8BA1;&#x6570;&#x5668;&#x3002;   </p>
<p><strong>&#x5F31;&#x6307;&#x9488;</strong>&#x7C7B;<code>wp</code>&#x7684;&#x5B9E;&#x73B0;  :</p>
<pre><code class="lang-c++">// frameworks\base\include\utils\RefBase.h

// wp&#x7C7B;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x7C7B;&#xFF0C; &#x6A21;&#x677F;&#x53C2;&#x6570;T&#x8868;&#x793A;&#x5BF9;&#x8C61;&#x7684;&#x5B9E;&#x9645;&#x7C7B;&#x578B;&#xFF0C; &#x5B83;&#x5FC5;&#x987B;&#x662F;&#x4ECE;RefBase&#x7C7B;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#x7684;
template &lt;typename T&gt;
class wp
{
public:
    typedef typename RefBase::weakref_type weakref_type;

    inline wp() : m_ptr(0) { }

    wp(T* other);
    wp(const wp&lt;T&gt;&amp; other);
    wp(const sp&lt;T&gt;&amp; other);
    template&lt;typename U&gt; wp(U* other);
    template&lt;typename U&gt; wp(const sp&lt;U&gt;&amp; other);
    template&lt;typename U&gt; wp(const wp&lt;U&gt;&amp; other);

    ~wp();

    // Assignment

    wp&amp; operator = (T* other);
    wp&amp; operator = (const wp&lt;T&gt;&amp; other);
    wp&amp; operator = (const sp&lt;T&gt;&amp; other);

    template&lt;typename U&gt; wp&amp; operator = (U* other);
    template&lt;typename U&gt; wp&amp; operator = (const wp&lt;U&gt;&amp; other);
    template&lt;typename U&gt; wp&amp; operator = (const sp&lt;U&gt;&amp; other);

    void set_object_and_refs(T* other, weakref_type* refs);

    // promotion to sp
    // &#x5C06;&#x8FD9;&#x4E2A;&#x5F31;&#x6307;&#x9488;&#x5347;&#x7EA7;&#x4E3A;&#x5F3A;&#x6307;&#x9488;
    // &#x5347;&#x7EA7;&#x6210;&#x529F;&#xFF0C;&#x5C31;&#x8BF4;&#x660E;&#x8BE5;&#x5F31;&#x6307;&#x9488;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x8FD8;&#x6CA1;&#x6709;&#x88AB;&#x9500;&#x6BC1;&#xFF0C; &#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x4F7F;&#x7528;
    sp&lt;T&gt; promote() const;

    // Reset

    void clear();

    // Accessors

    inline  weakref_type* get_refs() const { return m_refs; }

    inline  T* unsafe_get() const { return m_ptr; }

    // Operators

    COMPARE_WEAK(==)
    COMPARE_WEAK(!=)
    COMPARE_WEAK(&gt;)
    COMPARE_WEAK(&lt;)
    COMPARE_WEAK(&lt;=)
    COMPARE_WEAK(&gt;=)

    inline bool operator == (const wp&lt;T&gt;&amp; o) const 
    {
        return (m_ptr == o.m_ptr) &amp;&amp; (m_refs == o.m_refs);
    }
    template&lt;typename U&gt;
    inline bool operator == (const wp&lt;U&gt;&amp; o) const 
    {
        return m_ptr == o.m_ptr;
    }

    inline bool operator &gt; (const wp&lt;T&gt;&amp; o) const 
    {
        return (m_ptr == o.m_ptr) ? (m_refs &gt; o.m_refs) : (m_ptr &gt; o.m_ptr);
    }
    template&lt;typename U&gt;
    inline bool operator &gt; (const wp&lt;U&gt;&amp; o) const 
    {
        return (m_ptr == o.m_ptr) ? (m_refs &gt; o.m_refs) : (m_ptr &gt; o.m_ptr);
    }

    inline bool operator &lt; (const wp&lt;T&gt;&amp; o) const 
    {
        return (m_ptr == o.m_ptr) ? (m_refs &lt; o.m_refs) : (m_ptr &lt; o.m_ptr);
    }
    template&lt;typename U&gt;
    inline bool operator &lt; (const wp&lt;U&gt;&amp; o) const 
    {
        return (m_ptr == o.m_ptr) ? (m_refs &lt; o.m_refs) : (m_ptr &lt; o.m_ptr);
    }

    inline bool operator != (const wp&lt;T&gt;&amp; o) const 
    { 
        return m_refs != o.m_refs; 
    }
    template&lt;typename U&gt; inline bool operator != (const wp&lt;U&gt;&amp; o) const 
    { 
        return !operator == (o); 
    }

    inline bool operator &lt;= (const wp&lt;T&gt;&amp; o) const 
    { 
        return !operator &gt; (o); 
    }
    template&lt;typename U&gt; inline bool operator &lt;= (const wp&lt;U&gt;&amp; o) const 
    { 
        return !operator &gt; (o); 
    }

    inline bool operator &gt;= (const wp&lt;T&gt;&amp; o) const 
    { 
        return !operator &lt; (o); 
    }
    template&lt;typename U&gt; inline bool operator &gt;= (const wp&lt;U&gt;&amp; o) const 
    { 
        return !operator &lt; (o); 
    }

private:
    template&lt;typename Y&gt; friend class sp;
    template&lt;typename Y&gt; friend class wp;

    // &#x6307;&#x5411;&#x5B83;&#x6240;&#x5F15;&#x7528;&#x7684;&#x5BF9;&#x8C61;
    T*              m_ptr;
    // &#x7EF4;&#x62A4;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;
    weakref_type*   m_refs;
};
</code></pre>
<p>wp&#x7C7B;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;  :</p>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\include\utils\RefBase.h</span>

<span class="hljs-comment">// &#x6A21;&#x5757;&#x53C2;&#x6570;T&#x662F;&#x4E00;&#x4E2A;&#x7EE7;&#x627F;&#x4E86;RefBase&#x7C7B;&#x7684;&#x5B50;&#x7C7B;</span>
<span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> T&gt;
wp&lt;T&gt;::wp(T* other)
    : m_ptr(other)
{
    <span class="hljs-keyword">if</span> (other) 
    {
        <span class="hljs-comment">// &#x8C03;&#x7528; RefBase &#x7C7B;&#x7684;&#x6210;&#x5458;&#x51FD;&#x6570; createWeak &#x6765;&#x589E;&#x52A0;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
        m_refs = other-&gt;createWeak(<span class="hljs-keyword">this</span>);
    }
}
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">// frameworks\base\libs\utils\RefBase.cpp</span>

<span class="hljs-comment">// &#x5C06;&#x5B83;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF;mRefs&#x6240;&#x6307;&#x5411;&#x7684;&#x4E00;&#x4E2A; weakref_impl &#x5BF9;&#x8C61;&#x8FD4;&#x56DE;&#x7ED9;&#x8C03;&#x7528;&#x8005;</span>
RefBase::weakref_type* RefBase::createWeak(<span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span>* id) <span class="hljs-keyword">const</span>
{
    <span class="hljs-comment">// RefBase&#x7C7B;&#x7684;&#x6210;&#x5458;&#x53D8;&#x91CF; mRefs &#x6307;&#x5411;&#x7684;&#x662F;&#x4E00;&#x4E2A; weakref_impl &#x5BF9;&#x8C61;</span>
    <span class="hljs-comment">// &#x52A0;&#x5B9E;&#x9645;&#x5F15;&#x7528;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x8BA1;&#x6570;</span>
    mRefs-&gt;incWeak(id);
    <span class="hljs-keyword">return</span> mRefs;
}
</code></pre>
<p>wp&#x7C7B;&#x7684;&#x6790;&#x6784;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;  :</p>
<h3 id="&#x5E94;&#x7528;&#x5B9E;&#x4F8B;&#x5206;&#x6790;">&#x5E94;&#x7528;&#x5B9E;&#x4F8B;&#x5206;&#x6790;</h3>
<p>&#x5728;external&#x76EE;&#x5F55;&#x4E0B;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;C++&#x5E94;&#x7528;&#x7A0B;&#x5E8F;weightpointer&#x6765;&#x8BF4;&#x660E;&#x5F3A;&#x6307;&#x9488;&#x548C;&#x5F31;&#x6307;&#x9488;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;  </p>
<p>&#x76EE;&#x5F55;&#x7ED3;&#x6784;  :</p>
<pre><code class="lang-shell">~/Android
    external
        weightpointer
            weightpointer.cpp
            Android.mk
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; dsx2016.com 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x7AE0;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-07-13 21:18:00
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="hardware_abstraction_layer.html" class="navigation navigation-prev " aria-label="Previous page: 硬件抽象层">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="AOSP.html" class="navigation navigation-next " aria-label="Next page: AOSP源码开发">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"智能指针","level":"3.2","depth":1,"next":{"title":"AOSP源码开发","level":"3.3","depth":1,"path":"android_bottom/AOSP.md","ref":"android_bottom/AOSP.md","articles":[]},"previous":{"title":"硬件抽象层","level":"3.1","depth":1,"path":"android_bottom/hardware_abstraction_layer.md","ref":"android_bottom/hardware_abstraction_layer.md","articles":[]},"dir":"ltr"},"config":{"plugins":["back-to-top-button","chapter-fold","code","splitter","-lunr","-search","search-pro","insert-logo","custom-favicon","pageview-count","tbfed-pagefooter","popup","-sharing","sharing-plus","livereload"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy dsx2016.com 2019","modify_label":"该文章修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"popup":{},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":"./icon/book.ico","back-to-top-button":{},"pageview-count":{},"custom-favicon":{},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":true,"facebook":true,"weibo":true,"twitter":true,"pocket":true,"google":true,"qzone":true},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"insert-logo":{"style":"background: none; max-height: 30px; min-height: 30px","url":"https://s1.ax1x.com/2020/06/18/Nnpxmj.png"}},"theme":"default","author":"cpucode","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"GItBook笔记","language":"zh-hans","output.name":"site","links":{"sidebar":{"首页":"https://blog.csdn.net/qq_44226094"}},"gitbook":"3.2.3","description":"GitBook笔记"},"file":{"path":"android_bottom/smart_pointer.md","mtime":"2020-07-13T13:18:00.724Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-15T02:25:37.570Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

